/**
 * @author Simon Richardson - me@simonrichardson.info
 */
package org.osflash.signals.natives.sets;

import flash.events.Event;
import org.osflash.signals.natives.NativeSignal;
import asunit.asserts.AssertNotNull;
import asunit.asserts.AssertTrue;
import asunit.framework.IAsync;
import flash.display.Sprite;

class NativeSignalSetTest {

	@:meta(Inject())
	public var async : IAsync;
	var sprite : Sprite;
	var signalSet : NativeSignalSet;
	@:meta(Before())
	public function setUp() : Void {
		sprite = new Sprite();
		signalSet = new NativeSignalSet(sprite);
	}

	@:meta(After())
	public function tearDown() : Void {
		signalSet.removeAll();
		signalSet = null;
		sprite = null;
	}

	function newEmptyHandler() : Dynamic {
		return function(e : Dynamic) : Void {
		}
;
	}

	//////
	@:meta(Test())
	public function numListeners_should_be_zero() : Void {
		assertTrue("Number of listeners should be 0.", signalSet.numListeners == 0);
	}

	//////
	@:meta(Test())
	public function signals_should_be_not_null() : Void {
		assertNotNull("Signals should not be null.", signalSet.signals);
	}

	//////
	@:meta(Test())
	public function signals_length_should_be_empty() : Void {
		assertTrue("Signals length should be 0.", signalSet.signals.length == 0);
	}

	//////
	@:meta(Test())
	public function signals_length_should_be_zero_after_removeAll() : Void {
		signalSet.removeAll();
		assertTrue("Signals length should be 0.", signalSet.signals.length == 0);
	}

	//////
	@:meta(Test())
	public function numListeners_should_be_zero_after_removeAll() : Void {
		signalSet.removeAll();
		assertTrue("Number of listeners should be 0.", signalSet.numListeners == 0);
	}

	//////
	@:meta(Test())
	public function getNativeSignal_should_return_NativeSignal() : Void {
		var result : NativeSignal = signalSet.getNativeSignal(Event.INIT);
		assertTrue("getNativeSignal should return type NativeSignal.", Std.is(result, NativeSignal));
	}

	//////
	@:meta(Test())
	public function getNativeSignal_should_not_throw_Error_when_null_is_used_as_classType() : Void {
		signalSet.getNativeSignal(Event.INIT, null);
	}

	//////
	@:meta(Test(expects="ArgumentError"))
	public function getNativeSignal_should_throw_Error_when_used_for_event_type() : Void {
		signalSet.getNativeSignal(null);
	}

	//////
	@:meta(Test())
	public function getNativeSignal_should_increment_num_signals_to_one() : Void {
		signalSet.getNativeSignal(Event.INIT);
		assertTrue("Number of Signals should be 1", signalSet.signals.length == 1);
	}

	//////
	@:meta(Test())
	public function getNativeSignal_with_same_event_type_should_have_signal_length_of_one() : Void {
		signalSet.getNativeSignal(Event.INIT);
		signalSet.getNativeSignal(Event.INIT);
		assertTrue("Number of Signals should be 1", signalSet.signals.length == 1);
	}

	//////
	@:meta(Test())
	public function getNativeSignal_with_different_eventType_should_increment_num_signals_to_two() : Void {
		signalSet.getNativeSignal(Event.INIT);
		signalSet.getNativeSignal(Event.ACTIVATE);
		assertTrue("Number of Signals should be 2", signalSet.signals.length == 2);
	}

	//////
	@:meta(Test())
	public function getNativeSignal_with_lots_of_different_eventType_should_increment_num_signals_to_100() : Void {
		var i : Int = 0;
		while(i < 100) {
			signalSet.getNativeSignal("event" + i);
			i++;
		}
		assertTrue("Number of Signals should be 100", signalSet.signals.length == 100);
	}

	//////
	@:meta(Test())
	public function get_lots_of_getNativeSignal_then_removeAll_should_have_zero_signals() : Void {
		var i : Int = 0;
		while(i < 100) {
			signalSet.getNativeSignal("event" + i);
			i++;
		}
		signalSet.removeAll();
		assertTrue("Number of Signals should be 0", signalSet.signals.length == 0);
	}

	//////
	@:meta(Test())
	public function getNativeSignal_and_add_listener() : Void {
		var nativeSignal : NativeSignal = signalSet.getNativeSignal(Event.INIT);
		nativeSignal.add(newEmptyHandler());
		assertTrue("Number of listeners should be 1.", signalSet.numListeners == 1);
	}

	//////
	@:meta(Test())
	public function getNativeSignal_and_add_10_listeners() : Void {
		var nativeSignal : NativeSignal = signalSet.getNativeSignal(Event.INIT);
		var i : Int = 0;
		while(i < 10) {
			nativeSignal.add(newEmptyHandler());
			i++;
		}
		assertTrue("Number of listeners should be 10.", signalSet.numListeners == 10);
	}

	//////
	@:meta(Test())
	public function getNativeSignal_and_add_10_listeners_and_removeAll() : Void {
		var nativeSignal : NativeSignal = signalSet.getNativeSignal(Event.INIT);
		var i : Int = 0;
		while(i < 10) {
			nativeSignal.add(newEmptyHandler());
			i++;
		}
		signalSet.removeAll();
		assertTrue("Number of listeners should be 0.", signalSet.numListeners == 0);
	}

	//////
	@:meta(Test())
	public function getNativeSignal_and_add_10_listeners_and_removeAll_from_signal() : Void {
		var nativeSignal : NativeSignal = signalSet.getNativeSignal(Event.INIT);
		var i : Int = 0;
		while(i < 10) {
			nativeSignal.add(newEmptyHandler());
			i++;
		}
		nativeSignal.removeAll();
		assertTrue("Number of listeners should be 0.", signalSet.numListeners == 0);
	}

	//////
	@:meta(Test())
	public function get_two_getNativeSignal_and_add_10_listeners_to_each() : Void {
		var nativeSignal0 : NativeSignal = signalSet.getNativeSignal(Event.INIT);
		var i : Int = 0;
		i = 0;
		while(i < 10) {
			nativeSignal0.add(newEmptyHandler());
			i++;
		}
		var nativeSignal1 : NativeSignal = signalSet.getNativeSignal(Event.CHANGE);
		i = 0;
		while(i < 10) {
			nativeSignal1.add(newEmptyHandler());
			i++;
		}
		assertTrue("Number of listeners should be 20.", signalSet.numListeners == 20);
	}

	//////
}

